﻿using System;
using System.Collections.Generic;
using System.IO;
using DeepZoomPivotConstructor.Utilities;

namespace DeepZoomPivotConstructor.VisualLib
{
    /// <summary>
    /// Holds a single image.
    /// </summary>
    [Serializable]
    public class VisualSingleImage : Visual
    {
        private SceneGraphSceneNode _myimage = null;

        /// <summary>
        /// Create the VisualSingleImage. Use dotsperinch to convert
        /// to a coordinate system that is more "natural" to deal with.
        /// </summary>
        /// <param name="image">The actual jpeg (or whatever) image</param>
        /// <param name="dotsperinch">DPI - 600 for example</param>
        public VisualSingleImage(FileInfo image, int dotsperinch)
        {
            _myimage = CreateSceneImageFromImage(image, dotsperinch);
        }

        /// <summary>
        /// Given a particular image, create the scene graph guy. Get width
        /// and height right, of course, but make sure that the thing is sitting
        /// at (0,0).
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private SceneGraphSceneNode CreateSceneImageFromImage(FileInfo fname, int dpi)
        {
            SceneGraphSceneNode sg = new SceneGraphSceneNode() {
                FileName = fname.FullName,
                x = 0.0F, y = 0.0F,
                ZOrder = 1,
            };
            
            /// Load up the image. There is probably a better way to do this, without
            /// having to load in all the memory, however this guy means we support
            /// everything the .NET framework does for free.

            int[] sizes = Image.GetImageSize(fname);
            
            sg.Width = ((float) sizes[0] / (float) dpi);
            sg.Height = ((float) sizes[1] / (float) dpi);

            return sg;
        }

        /// <summary>
        /// Returns the widht in inches.
        /// </summary>
        public override double Width
        {
            get {return _myimage.Width;}
        }

        /// <summary>
        /// Returns the height in inches.
        /// </summary>
        public override double Height
        {
            get { return _myimage.Height; }
        }

        public override double XPosition
        {
            get
            {
                return _myimage.x;
            }
            set
            {
                _myimage.x = value;
            }
        }

        public override double YPosition
        {
            get
            {
                return _myimage.y;
            }
            set
            {
                _myimage.y = value;
            }
        }

        /// <summary>
        /// Scale the size of the image. This is pretty easy!
        /// </summary>
        /// <param name="factor"></param>
        public override void Scale(double factor)
        {
            _myimage.Width *= factor;
            _myimage.Height *= factor;
        }

        /// <summary>
        /// Returns the list of one!
        /// </summary>
        public override List<SceneGraphSceneNode> SGNodeList
        {
            get {
                List<SceneGraphSceneNode> nodes = new List<SceneGraphSceneNode>();
                nodes.Add(_myimage);
                return nodes;
            }
        }

        /// <summary>
        /// Change the z index by some amount.
        /// </summary>
        /// <param name="delta"></param>
        public override void IncZIndex(int delta)
        {
            _myimage.ZOrder += delta;
        }

        public override int ZIndex
        {
            get { return _myimage.ZOrder; }
        }
    }
}
